home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / gui / SnmpTable.java < prev    next >
Text File  |  1997-06-09  |  3KB  |  137 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. /**
  16.  * Class Description
  17.  * @see 
  18.  * @version     $Id: SnmpTable.java,v 1.4 1997/06/09 09:22:58 alex Exp $
  19.  * @author      Alex Kowalenko
  20.  */
  21.  
  22. package aka.gui;
  23.  
  24. import java.util.*;
  25. import aka.snmp.*;
  26.  
  27. class SnmpTable extends Observable implements Runnable {
  28.  
  29.     private String _host; 
  30.     private String _communityName = "public";
  31.     private int _polling = 10;
  32.  
  33.     private Vector _objects;
  34.     private Snmp _snmp;
  35.     
  36.   public SnmpTable() {
  37.       _objects = new Vector();
  38.  
  39.       // Set up snmp
  40.  
  41.       _snmp = new Snmp();
  42.       try {
  43.       _snmp.readFile("mibs.ser");
  44.       } catch (Exception e) {
  45.       System.err.println("Problem reading mib file " + e.getMessage());
  46.       System.err.flush();
  47.       return;
  48.       }
  49.       _snmp.setTimeOut(3);
  50.       _snmp.setCommunityName(_communityName);
  51.       
  52.       // Start polling
  53.       String value[] = new String[2];
  54.       value[0] = "sysName"; value[1] = "";
  55.       _objects.addElement(value);
  56.       String value2[] = new String[2];
  57.       value2[0] = "sysUpTime"; value2[1] = "";
  58.       _objects.addElement(value2);
  59.       (new Thread(this)).start();
  60.   }
  61.  
  62.   public void setHost(String host) {
  63.       _host = host;
  64.       return;
  65.   };
  66.  
  67.   public void setCommunityName(String name) {
  68.       _communityName = name;
  69.       _snmp.setCommunityName(name);
  70.       return;
  71.   };
  72.     
  73.   public void quit() {
  74.       System.out.println("Quit");
  75.   };
  76.  
  77.   public void addVariable(String name) {
  78.       String value[] = new String[2];
  79.       value[0] = name; value[1] = "";
  80.       _objects.addElement(value); 
  81.       poll();
  82.       return;
  83.   };
  84.  
  85.   public void deleteVariable(String name) {
  86.       for(Enumeration e = _objects.elements(); e.hasMoreElements();) {
  87.       String object[] = (String[]) e.nextElement();
  88.       if(name.startsWith(object[0])) {
  89.           _objects.removeElement(object);
  90.           poll();
  91.       };
  92.       };
  93.   };
  94.  
  95.   public void setPollingInterval(int poll) {
  96.       _polling = poll;
  97.       return;
  98.   };
  99.    
  100.   public Enumeration getObjects() {
  101.       return _objects.elements();
  102.   };
  103.  
  104.   private synchronized void poll() {
  105.       // get values
  106.       for(Enumeration e  = _objects.elements(); e.hasMoreElements();) {
  107.       String[] object = (String[]) e.nextElement();
  108.       try {
  109.           Type resultVal = _snmp.get(_host, object[0], 0);
  110.           object[1] = resultVal.toString();
  111.       } catch(Exception ex) {
  112.           object[1] = "Exception: " + ex.getMessage();
  113.       };
  114.       };
  115.       setChanged();
  116.       notifyObservers();
  117.       clearChanged();
  118.   };
  119.      
  120.   public void run() {
  121.       
  122.       while(true) {
  123.       poll();
  124.  
  125.       // sleep polling interval
  126.       try {
  127.           Thread.currentThread().sleep(_polling * 1000);
  128.       } catch(Exception e) {};
  129.       };
  130.   };
  131.  
  132.   public static void main(String args[]) {
  133.       SnmpTable app = new SnmpTable();
  134.       SnmpTableGui gui = new SnmpTableGui(app);
  135.   };
  136. }
  137.